39 Lecture

CS201

Midterm & Final Term Short Notes

Pointers

In C++, pointers are variables that store memory addresses. They are used to manipulate memory directly and to create complex data structures like linked lists, trees, and graphs. Pointers can be declared and initialized like any other variable,


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the operator used to declare a pointer in C++? A) & B) * C) $ D) %

Answer: B

  1. What is the difference between a null pointer and a void pointer in C++? A) A null pointer points to nothing, while a void pointer points to any data type. B) A null pointer points to a memory address, while a void pointer points to a function. C) A null pointer is used for deallocation, while a void pointer is used for allocation. D) A null pointer is used for function pointers, while a void pointer is used for object pointers.

Answer: A

  1. What is the correct syntax for dynamically allocating memory for a pointer in C++? A) int* p = malloc(sizeof(int)); B) int* p = new int; C) int p = new int; D) int* p = malloc(int);

Answer: B

  1. What is a dangling pointer in C++? A) A pointer that points to a valid memory address. B) A pointer that points to a deallocated memory address. C) A pointer that points to a null memory address. D) A pointer that points to a function.

Answer: B

  1. What is the purpose of the const keyword when working with pointers in C++? A) To declare a constant pointer variable. B) To declare a pointer to a constant variable. C) To declare a constant memory address. D) To declare a constant value pointed to by a pointer.

Answer: B

  1. What is the difference between a reference and a pointer in C++? A) A reference is an alias for a variable, while a pointer is a separate variable that stores a memory address. B) A reference is a separate variable that stores a memory address, while a pointer is an alias for a variable. C) A reference and a pointer are the same thing in C++. D) A reference is used for dynamic memory allocation, while a pointer is used for static memory allocation.

Answer: A

  1. What is the operator used to access the value pointed to by a pointer in C++? A) . B) -> C) & D) *

Answer: D

  1. What is the correct syntax for deleting a dynamically allocated pointer in C++? A) delete p; B) delete *p; C) delete &p; D) free(p);

Answer: A

  1. What is a memory leak in C++? A) A pointer that points to a deallocated memory address. B) A pointer that points to a null memory address. C) A pointer that points to a valid memory address. D) A failure to deallocate dynamically allocated memory, causing the program to use up all available memory.

Answer: D

  1. What is a smart pointer in C++? A) A pointer that automatically deallocates memory when it goes out of scope. B) A pointer that automatically allocates memory when it is assigned a value. C) A pointer that automatically sets the value pointed to by the pointer to null. D) A pointer that automatically dereferences itself when used in code.

Answer: A



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a pointer in C++? A pointer is a variable that stores the memory address of another variable.

  2. How can you declare a pointer variable in C++? A pointer variable is declared by placing an asterisk (*) before the variable name.

  3. How do you initialize a pointer to point to a specific memory address? You can initialize a pointer by assigning the address of a variable to it using the reference operator (&).

  4. How do you access the value of the variable that a pointer is pointing to? You can access the value of the variable that a pointer is pointing to using the dereference operator (*).

  5. What is the difference between a null pointer and a void pointer? A null pointer is a pointer that does not point to any memory location, while a void pointer is a pointer that can point to any data type.

  6. What is pointer arithmetic? Pointer arithmetic refers to the arithmetic operations that can be performed on pointers, such as adding or subtracting an integer value to a pointer.

  7. What is a function pointer? A function pointer is a pointer that points to a function instead of a variable.

  8. What is a dangling pointer? A dangling pointer is a pointer that points to a memory location that has been deallocated or freed.

  9. How do you avoid memory leaks when using pointers? You can avoid memory leaks by ensuring that you deallocate any dynamically allocated memory using the delete operator.

  10. How do you use pointers to pass arguments to a function by reference? You can use pointers to pass arguments to a function by reference by passing the address of the variable to the function and then using the dereference operator to access the value of the variable.

In C++, a pointer is a variable that stores the memory address of another variable. Pointers are an important concept in programming because they allow you to work with memory directly, which can be useful for tasks such as dynamic memory allocation, passing arguments to functions by reference, and manipulating arrays. To declare a pointer variable in C++, you place an asterisk (*) before the variable name, like this:
c++
int *ptr;
This declares a pointer variable called ptr that can store the memory address of an integer variable. To initialize a pointer to point to a specific memory address, you use the reference operator (&) to get the address of a variable, like this:
c++
int num = 42; int *ptr = #
This initializes the pointer ptr to point to the memory address of the variable num. To access the value of the variable that a pointer is pointing to, you use the dereference operator (*) to retrieve the value from the memory location pointed to by the pointer, like this:
c++
int num = 42; int *ptr = &num; std::cout << *ptr << std::endl; // prints "42"
Pointer arithmetic refers to the arithmetic operations that can be performed on pointers, such as adding or subtracting an integer value to a pointer. This can be useful for tasks such as iterating over an array. Function pointers are pointers that point to functions instead of variables. They can be useful for tasks such as passing functions as arguments to other functions or implementing callback functions. Dangling pointers are pointers that point to a memory location that has been deallocated or freed. They can be a source of bugs and memory leaks in a program, so it's important to be careful when working with pointers and to ensure that any dynamically allocated memory is properly deallocated using the delete operator.